<?php
/**
 * Created by PhpStorm.
 * User: gyccvvh
 * Date: 2018-06-28
 * Time: 14:08
 */

namespace app\merchant\controller;


use app\merchant\define\Merchant;
use think\Request;

class Goods extends Basic
{
    protected $goods_service = null;

    public function _initialize()
    {
        parent::_initialize();
        $arr[0]['text']   = '商品列表';
        $arr[0]['url']    = 'index';
        $this->assign('third_menu', $arr);

    }

    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->goods_service = new \app\merchant\service\Goods();
    }

    public function index(){
        $_form = input('');
        $ret = $this->goods_service->getList($this->merchant['id'],[
            ["name","like"]
        ],$_form);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg());
        }
        $records = $ret->getData("goods");
        $this->assign("records",$records);
        $this->assign("where",$_form);
        return $this->fetch();
    }

    public function add() {
        if($this->request->isPost()){
            $_form = select_object("name,keyword,origcost,price,express_id,stock,images,state");
            $_form['mer_id'] = $this->merchant['id'];
            $_spec_name = input('spec_name/a',[]);
            $_spec_price = input('spec_price/a',[]);
            $_color_name = input('color_name/a',[]);
            $_color_img = input('color_img/a',[]);
            $_color_price = input('color_price/a',[]);
            $scene = input('scene/a',[]);
            $intended = input('intended/a',[]);
            //保存商品信息
            $ret = $this->goods_service->saveGoods($_form, $_spec_name, $_spec_price, $_color_name, $_color_img, $_color_price, $scene, $intended);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }else {
                return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
            }
        }else{
            $this->assign("mer_id",$this->merchant['id']);
            return $this->fetch();
        }
    }
    public function edit() {
        if($this->request->isPost()) {
            $_form = select_object("id,name,keyword,origcost,price,express_id,stock,images,state");
            $_form['mer_id'] = $this->merchant['id'];
            $_spec_name = input('spec_name/a',[]);
            $_spec_price = input('spec_price/a',[]);
            $_color_name = input('color_name/a',[]);
            $_color_img = input('color_img/a',[]);
            $_color_price = input('color_price/a',[]);
            $scene = input('scene/a',[]);
            $intended = input('intended/a',[]);
            $ret = $this->goods_service->updateGoods($_form, $_spec_name, $_spec_price, $_color_name, $_color_img, $_color_price, $scene, $intended);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }else {
                return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
            }
        }else {
            $id = input("id");
            $ret = $this->goods_service->getOne($this->merchant['id'],$id);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                $this->error($ret->getMsg());
            }
            $good = $ret->getData("one");
            $this->assign("good",$good);
            $this->assign("mer_id",$this->merchant['id']);
            return $this->fetch();
        }
    }

    public function del() {

    }

    public function designer() {
        if($this->request->isPost()) {
            $_form = input("");
            $ret = $this->goods_service->updateDescript($_form['id'],$_form['description_url'],$_form['description']);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(1)->setMsg($ret->getMsg())->toJson();
            }
            return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
        }else {
            $id = input("id");
            $ret = $this->goods_service->getOne($this->merchant['id'],$id);
            if($ret->getCode() == Merchant::$Code_Mer_Error) {
                return $this->ret->setCode(0)->setMsg($ret->getMsg())->toJson();
            }
            $one = $ret->getData("one");
            $this->assign("one",$one);
            return $this->fetch();
        }
    }

    public function puton() {
        $_form = input('');
        $_form['state'] = 1;
        $ret = $this->goods_service->getList($this->merchant['id'],[
            ["name","like"],
            ["state"]
        ],$_form);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            $this->error($ret->getMsg());
        }
        $records = $ret->getData("goods");
        $this->assign("records",$records);
        $this->assign("where",$_form);
        return $this->fetch();
    }

    public function putoff() {
        $id = input('id');
        $ret = $this->goods_service->putoff($id);
        if($ret->getCode() == Merchant::$Code_Mer_Error) {
            return $this->ret->setCode(1)->setMsg(Merchant::$Msg_Mer_Error)->toJson();
        }
        return $this->ret->setCode(0)->setMsg($ret->getMsg())->setReload()->toJson();
    }


    public function types() {

    }
}